package com.hb.znpt.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.hb.znpt.activity.LeaseAddActivity;
import com.hb.znpt.activity.MainActivity;
import com.hb.znpt.bean.LeaseBean;
import com.hb.znpt.bean.ShareBean;
import com.hb.znpt.model.LeaseModel;
import com.hb.znpt.utils.Constants;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.SPUtils;
import com.unitlib.base.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/12/17 0:20
 */
public class LeaseViewModel extends BaseViewModel<LeaseModel> {

    public MutableLiveData<List<LeaseBean>> listData = new MutableLiveData<>();

    public LeaseViewModel(@NonNull Application application) {
        super(application);
        model = new LeaseModel();
    }

    public void add(View view) {
        startActivity(LeaseAddActivity.class);
    }

    @Override
    public void onResume() {
        model.getLeaseList()
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    getUC().getShowLoadingDialogEvent().call();
                })
                .doFinally(() -> {
                    getUC().getDismissLoadingDialogEvent().call();
                })
                .as(RxLife.as(owner))
                .subscribe(been -> {
                    List<LeaseBean> beans = new ArrayList<>();
                    for (int i = 0; i < been.size(); i++) {
                        if (!(been.get(i).getFpflag().equals("1") || been.get(i).getFpflag().equals("0"))) {
                            beans.add(been.get(i));
                        }
                    }
                    listData.setValue(beans);
                }, error -> {
                    ToastUtils.showShort(error.getMessage());
                });
    }
}
